package com.piper.demo.controller;

import com.piper.demo.service.IDemoService;
import com.piper.spring.annotation.Autowired;
import com.piper.spring.annotation.Controller;
import com.piper.spring.annotation.RequestMapping;
import com.piper.spring.model.RequestData;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class DemoController {

    @Autowired
    private IDemoService demoService;

    @RequestMapping("/")
    public void index(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        resp.sendRedirect(req.getContextPath() + "/index.html");
    }

    @RequestMapping("/demo/query")
    public void query(HttpServletRequest req, HttpServletResponse resp, RequestData request) {
        String name = request.getString("name");
        String res = demoService.get(name);
        try {
            resp.getWriter().println(res);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
